package com.ldl.origami.origamiService;

import com.ldl.origami.dao.TutorialMapper;
import com.ldl.origami.dao.TutorialStoreMapper;
import com.ldl.origami.domain.Tutorial;
import com.ldl.origami.domain.TutorialStore;
import com.ldl.origami.domain.TutorialStoreExample;
import com.ldl.origami.domain.TutorialStorePage;
import com.ldl.origami.util.OrigamiUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/9/19 11:06
 */
@Service
public class TutorialStoreService {

    static org.slf4j.Logger logger = LoggerFactory.getLogger(TutorialStoreService.class);
    @Autowired(required = false)
    private TutorialStoreMapper tutorialStoreMapper;
    @Autowired(required = false)
    private TutorialMapper tutorialMapper;


    /**
     * 查询userName收藏信息
     * @param userName
     * @return
     * @throws Exception
     */
    public List<TutorialStore> getTutorialStoreByUser(String userName) throws Exception {

        List<TutorialStore> tutorialStores = null;

        TutorialStoreExample example = new TutorialStoreExample();
        example.createCriteria().andUsernameEqualTo(userName);

        try {
            tutorialStores = tutorialStoreMapper.selectByExample(example);
        } catch (Exception e) {
            logger.debug("查询用户收藏失败:" + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }

        return tutorialStores;
    }


    /**
     * 教程收藏
     * @param tutorialStore
     * @throws Exception
     */
    public void stored(TutorialStore tutorialStore,Tutorial tutorial) throws Exception {

        try {
            tutorialStoreMapper.insert(tutorialStore);
            tutorialMapper.updateByPrimaryKeySelective(tutorial);
        } catch (Exception e) {
            logger.debug("收藏失败: "+OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }

    /**
     * 取消教程收藏
     * @param userName
     * @param tutorialId
     * @throws Exception
     */
    public void unStored(String userName,int tutorialId,Tutorial tutorial) throws Exception {

        TutorialStoreExample tutorialStoreExample = new TutorialStoreExample();
        tutorialStoreExample.createCriteria().andUsernameEqualTo(userName).andTutorialidEqualTo(tutorialId);
        try {
            tutorialStoreMapper.deleteByExample(tutorialStoreExample);
            tutorialMapper.updateByPrimaryKeySelective(tutorial);
        } catch (Exception e) {
            logger.debug("取消收藏失败: "+OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
    }


    /**
     * 获取收藏的教程的id
     *
     * @return
     */
    public List<Integer> getMyCollectionsIdByPage(TutorialStorePage tutorialStorePage) throws Exception {

        List<Integer> tutorialsId = null;
        try {
            tutorialsId = tutorialStoreMapper.selectMyCollectionIdByPage(tutorialStorePage);
        } catch (Exception e) {
            logger.debug("查询我的收藏失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorialsId;
    }


    /**
     * 获取更多收藏的教程的id
     *
     * @return
     */
    public List<Integer> getMoreMyCollectionsIdByPage(TutorialStorePage tutorialStorePage) throws Exception {

        List<Integer> tutorialsId = null;
        try {
            tutorialsId = tutorialStoreMapper.selectMyCollectionIdByPage(tutorialStorePage);
        } catch (Exception e) {
            logger.debug("查询更多我的收藏失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorialsId;
    }
}
